package com.kuaikan.cnbeta.adapter;

import java.util.List;

import net.tsz.afinal.FinalBitmap;

import com.kuaikan.cnbeta.R;
import com.kuaikan.cnbeta.activity.NewsShowActivity;
import com.kuaikan.cnbeta.pojo.News;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;


public class NewsTitleAdapter extends BaseAdapter {
	public List<News> items;
	public Activity activity;
	Intent intenttoshowitem;
	private FinalBitmap fb;

	private NewsTitleAdapter() {
	}

	public NewsTitleAdapter(List<News> items, Activity activity) {
		this.items = items;
		this.activity = activity;
		 fb = FinalBitmap.create(activity);
	}

	// 先定义，ViewHolder静态类
	static class ViewHolder {
		public TextView tvTitle;
		public ImageView ivTitle;
		public LinearLayout llHolder;
	}

	@Override
	public int getCount() {
		// 在此适配器中所代表的数据集中的条目数
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// 获取数据集中与指定索引对应的数据项
		return position;
	}

	@Override
	public long getItemId(int position) {
		// 获取在列表中与指定索引对应的行id
		return position;
	}

	@Override
	// 获取一个在数据集中指定索引的视图来显示数据
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		// 如果缓存convertView为空，则需要创建View
		if (convertView == null) {
			holder = new ViewHolder();
			// 根据自定义的布局加载布局
			convertView = activity.getLayoutInflater().inflate(
					R.layout.adapter_news_title, null);
			holder.llHolder=(LinearLayout) convertView.findViewById(R.id.llHolder);
			holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
//			holder.tvSummary=(TextView) convertView.findViewById(R.id.tvSummary);
			holder.ivTitle=(ImageView) convertView.findViewById(R.id.ivTitle);
			// 将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 设置数据
		News item = items.get(position);
		this.setData(holder, position);
		holder.llHolder.setOnClickListener(new ShowItemClick(item));
		return convertView;
	}
	
	public void setData(ViewHolder holder,int position){
		News item=items.get(position);
		holder.tvTitle.setText(item.getTitle());
//		holder.tvSummary.setText(item.getSummary());
		fb.display(holder.ivTitle, item.getTitleImageUrl());
	}

	class ShowItemClick implements View.OnClickListener {
		public News showitem;

		private ShowItemClick() {
		}

		public ShowItemClick(News showitem) {
			this.showitem = showitem;
		}

		@Override
		public void onClick(View v) {
			intenttoshowitem = new Intent(activity, NewsShowActivity.class);
			intenttoshowitem.putExtra("news", showitem);
			
			activity.startActivity(intenttoshowitem);
		}
	}
}
